package org.jeecg.modules.stOrder.service.impl;

import org.jeecg.modules.stOrder.entity.HtStOrder;
import org.jeecg.modules.stOrder.entity.HtStOrderDetail;
import org.jeecg.modules.stOrder.mapper.HtStOrderDetailMapper;
import org.jeecg.modules.stOrder.mapper.HtStOrderMapper;
import org.jeecg.modules.stOrder.service.IHtStOrderService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 食堂订单
 * @Author: jeecg-boot
 * @Date:   2020-06-01
 * @Version: V1.0
 */
@Service
public class HtStOrderServiceImpl extends ServiceImpl<HtStOrderMapper, HtStOrder> implements IHtStOrderService {

	@Autowired
	private HtStOrderMapper htStOrderMapper;
	@Autowired
	private HtStOrderDetailMapper htStOrderDetailMapper;
	
	@Override
	@Transactional
	public void saveMain(HtStOrder htStOrder, List<HtStOrderDetail> htStOrderDetailList) {
		htStOrderMapper.insert(htStOrder);
		if(htStOrderDetailList!=null && htStOrderDetailList.size()>0) {
			for(HtStOrderDetail entity:htStOrderDetailList) {
				//外键设置
				entity.setOrderNo(htStOrder.getOrderno());
				htStOrderDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(HtStOrder htStOrder,List<HtStOrderDetail> htStOrderDetailList) {
		htStOrderMapper.updateById(htStOrder);
		
		//1.先删除子表数据
		htStOrderDetailMapper.deleteByMainId(htStOrder.getId());
		
		//2.子表数据重新插入
		if(htStOrderDetailList!=null && htStOrderDetailList.size()>0) {
			for(HtStOrderDetail entity:htStOrderDetailList) {
				//外键设置
				entity.setOrderNo(htStOrder.getOrderno());
				htStOrderDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		htStOrderDetailMapper.deleteByMainId(id);
		htStOrderMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			htStOrderDetailMapper.deleteByMainId(id.toString());
			htStOrderMapper.deleteById(id);
		}
	}
	
}
